package com.one.shopping.ui.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.one.shopping.R;
import com.one.shopping.adapter.ShopGoodsAdapter;
import com.one.shopping.entity.Goods;
import com.one.shopping.entity.ShoppingCart;
import com.one.shopping.ui.baseActivity.BaseActivity;
import com.one.shopping.ui.fragment.ShopGoodsFragment;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class ShoppingCartActivity extends BaseActivity implements OnItemLongClickListener{

//	private Button clearBtn;
	private ListView goodsLV;
//	private Button backBtn;
	private Button goBtn;
	public static ListAdapter adapter;
	private List<Goods> goodlist;
	public static TextView totalTv;
	private List<ShoppingCart> shoppingcartList;

	private ImageView clearBtn;

	@Override
	protected int getContentViewId() {
		return R.layout.activity_shopping_cart;
	}

	@Override
	protected void initView() {
		super.initView();
		setTitle("购物车");
		isVisibleReturn(true);

		Intent intent=getIntent();
		goodlist=(List<Goods>) intent.getSerializableExtra("shoppingcartList");
		clearBtn = (ImageView) findViewById(R.id.main_goods_clear_btn_iv);
		clearBtn.setVisibility(View.VISIBLE);
		clearBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				clearCart();//清空购物车
			}
			
		});
		
		
		totalTv = (TextView) findViewById(R.id.shopcartTotalTV);
		goodsLV=(ListView) findViewById(R.id.goodlist);
		goodsLV.setOnItemLongClickListener(this);
		if (goodlist!=null) {
			adapter=new ShopGoodsAdapter(ShoppingCartActivity.this, goodlist,ShopGoodsAdapter.SHOPPINGCART);
			goodsLV.setAdapter(adapter);
		}
		
//		backBtn= (Button) findViewById(R.id.backbtn);
		returnBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		goBtn=(Button) findViewById(R.id.gobtn);
		goBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (ShopGoodsFragment.total > 0) {
					Intent intent=new Intent(ShoppingCartActivity.this,OrderActivity.class);
//					intent.putExtra("userid", "");kzc

					intent.putExtra("total", ShopGoodsFragment.total);
					SharedPreferences mySharedPreferences = getSharedPreferences("shoppingcart", Activity.MODE_PRIVATE);
					String id = mySharedPreferences.getString("id","");
					String name = mySharedPreferences.getString("name", ""); 
					String image = mySharedPreferences.getString("image", "");
					String price = mySharedPreferences.getString("price", "");
					String quantity = mySharedPreferences.getString("quantity", "");
					
					shoppingcartList = new ArrayList<ShoppingCart>();
					if (id!="") {
						String ids[] = id.split(",");
						String names[] = name.split(",");
						String images[] = image.split(",");
						String prices[] = price.split(",");
						String quantitys[] = quantity.split(",");
						
						for (int i = 0; i < ids.length; i++) {
							if (ids[i].trim()!="0") {
								ShoppingCart shoppingCart = new ShoppingCart();
								shoppingCart.setId(ids[i]);
								shoppingCart.setName(names[i]);
								shoppingCart.setImage(images[i]);
								shoppingCart.setPrice(prices[i]);
								shoppingCart.setQuantity(Integer.valueOf(quantitys[i]));
								shoppingcartList.add(shoppingCart);
							}
						}
					}
					
					intent.putExtra("shoppingcartList", (Serializable)shoppingcartList);
					startActivity(intent);
				}else{
				}
			}
		});
	}
	
	/**
	 * 清除购物车
	 */
	private void clearCart() {
		SharedPreferences mySharedPreferences = getSharedPreferences(
				"shoppingcart", Activity.MODE_PRIVATE);
		SharedPreferences.Editor editor = mySharedPreferences.edit();
		editor.clear();  
        editor.commit();
        ShopGoodsFragment.goodBtnOnClickListener.onClick(null);
        goodlist=new ArrayList<Goods>();
        adapter=new ShopGoodsAdapter(ShoppingCartActivity.this, goodlist,ShopGoodsAdapter.SHOPPINGCART);
		goodsLV.setAdapter(adapter);
	}
	
	
	@Override
	public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
			final int arg2, long arg3) {
		// 弹出对话框删除
		AlertDialog.Builder builder = new Builder(ShoppingCartActivity.this);
		builder.setMessage("是否删除？");
		builder.setTitle("提示");
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO 自动生成的方法存根

				adapter=new ShopGoodsAdapter(ShoppingCartActivity.this, goodlist,ShopGoodsAdapter.SHOPPINGCART);
				goodsLV.setAdapter(adapter);

				arg0.dismiss();

			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				arg0.dismiss();

			}
		});
		builder.create().show();

		return true;
	}

}
